﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using YAPL.Language.Ast;
using YAPL.Util;

namespace YAPL.Language.SourceParser.XML.NodeParsers {
	public class XmlConditionalParser : XmlExpressionParser {
		public XmlConditionalParser(XmlInputParser parser) : base(parser) {}
		public override Expression ParseNode(XElement node) {
			var conditionNode = node.Element(XmlNames.ConditionalConditionExpression);
			Require.NotNull(conditionNode);
			var conditionExpr = Parser.ParseNode(conditionNode.Elements().First());

			var thenNode = node.Element(XmlNames.ConditionalThenExpression);
			Require.NotNull(thenNode);
			var thenExpr = (CodeBlockExpression)Parser.ParseNode(thenNode);

			CodeBlockExpression elseExpr = null;
			var elseNode = node.Element(XmlNames.ConditionalElseExpression);
			if (elseNode != null) {
				elseExpr = (CodeBlockExpression)Parser.ParseNode(elseNode);
			}
			return Expressions.Conditional(conditionExpr, thenExpr, elseExpr);
		}
	}
}
